package com.etc.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @Author: 史泽浩
 * @Date: 2019/5/21 17:29
 */
public class TelValidator {
    public static String telValidator(String tel) {
        String[] num = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        StringBuilder randomCode = new StringBuilder("");
        for (int i = 0; i < 6; i++) {
            String random = num[(int) Math.ceil(Math.random() * 9)];
            randomCode = randomCode.append(random);
        }
        String send = "{code :" + randomCode + "}";
        //System.out.println(send);
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI1vBt3rHyifRG", "EM0BKiJFv8anQBupuLbe9XK6T7dOeL");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "舒客旅游");
        request.putQueryParameter("TemplateCode", "SMS_166665647");
        request.putQueryParameter("TemplateParam", send);
        System.out.println(send);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return randomCode + "";
    }

    /*public static void main(String[] args) {
        System.out.println(telValidator("13074854769"));
    }*/
}
